﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;

namespace PuzzleGame
{
    public partial class PuzzleFrm : Form
    {
        public PuzzleFrm()
        {
            InitializeComponent();
        }

        int PointX = 0;
        int PointY = 0;
        int Imagecol = 0;
        int Imagerow = 0;
        int stepCount = 0;
        //BLL.OpreateImageBLL ob = new BLL.OpreateImageBLL();

        Image[,] images = null;
        private void MainForm_Load(object sender, EventArgs e)
        {

            radioButton1.Checked = true;
            label1.Text = "未开始 ";

        }

        private void MainForm_KeyPress(object sender, KeyPressEventArgs e)
        {
            //空图
            string key = (PointY * Imagecol + PointX).ToString();
            Control c = panel1.Controls.Find(key, true)[0];
            PictureBox pEmpty = null;
            if (c != null)
            {
                pEmpty = c as PictureBox;
            }
            //上 w 87/119
            if (e.KeyChar == 87 || e.KeyChar == 119)
            {
                if (PointY != (Imagerow - 1))
                {
                    PointY++;
                }


            }
            //下 s 83/115
            if (e.KeyChar == 83 || e.KeyChar == 115)
            {
                if (PointY != 0)
                {
                    PointY--;
                }
            }
            //左 a 65/97
            if (e.KeyChar == 65 || e.KeyChar == 97)
            {

                if (PointX != (Imagecol - 1))
                {
                    PointX++;
                }

            }
            //右 d 68/100
            if (e.KeyChar == 68 || e.KeyChar == 100)
            {

                if (PointX != 0)
                {
                    PointX--;
                }
            }
            //得到现在的空位置
            string keynow = (PointY * Imagecol + PointX).ToString();
            Control conNow = panel1.Controls.Find(keynow, true)[0];
            PictureBox pNow = conNow as PictureBox;
            //交换图片
            pEmpty.Image = pNow.Image;
            pNow.Image = null;
            pNow.BackColor = Color.Gray;
            stepCount++;
            if (BLL.OpreateImageBLL.CheckImageGamesComplate(panel1,Imagerow,Imagecol))
            {
                MessageBox.Show("恭喜完成！");
            }
            label1.Text = "你共走了 " + stepCount.ToString() + " 步！";
        }

        private void MainForm_KeyDown(object sender, KeyEventArgs e)
        {
            // MessageBox.Show(e.KeyCode.ToString());
        }
        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {

            textBox1.Text = panel1.Controls.Count.ToString();

            PointX = 0;
            PointY = 0;
            if (radioButton1.Checked)//3*3
            {
                BLL.OpreateImageBLL.DestoryPictureBox(panel1);//销毁panel1中的picturebox 对象
                Image img = global::PuzzleGame.Properties.Resources.tu;
                Imagecol = 3;
                Imagerow = 3;
                panel1.BackgroundImage = null;
                //画出3*3 的图片框
                BLL.OpreateImageBLL.CreateImageBoxsByRowAndColumn(img, Imagecol, Imagerow, panel1);
                images = BLL.OpreateImageBLL.images;
            }

            textBox2.Text = panel1.Controls.Count.ToString();
        }
        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton2.Checked)//3*4
            {
                BLL.OpreateImageBLL.DestoryPictureBox(panel1);//销毁panel1中的picturebox 对象
                Image img = global::PuzzleGame.Properties.Resources.tu;
                panel1.BackgroundImage = null;
                PointX = 0;
                PointY = 0;
                Imagecol = 3;
                Imagerow = 4;
                BLL.OpreateImageBLL.CreateImageBoxsByRowAndColumn(img, Imagecol, Imagerow, panel1);
                images = BLL.OpreateImageBLL.images;
            }
        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton3.Checked)//4*4
            {
                BLL.OpreateImageBLL.DestoryPictureBox(panel1);//销毁panel1中的picturebox 对象
                Image img = global::PuzzleGame.Properties.Resources.tu;
                panel1.BackgroundImage = null;
                PointX = 0;
                PointY = 0;
                Imagecol = 4;
                Imagerow = 4;
                BLL.OpreateImageBLL.CreateImageBoxsByRowAndColumn(img, Imagecol, Imagerow, panel1);
                images = BLL.OpreateImageBLL.images;
            }
        }
        //开始按钮
        private void btnStart_Click(object sender, EventArgs e)
        {
            PointX = 0;
            PointY = 0;
            Dictionary<string, Image> dic = new Dictionary<string, Image>();
            Random random = new Random();
            int diccount = 0;
            int c = random.Next(0, Imagecol);
            int r = random.Next(0, Imagerow);
            while (dic.Count <( Imagecol * Imagerow - 1))
            {
               
                if (c==0&&r==0)
                {
                    c = random.Next(0, Imagecol);
                    r = random.Next(0, Imagerow);
                    continue;
                }
                if (!dic.ContainsValue(images[r, c]))
                {
                    dic.Add(diccount.ToString(), images[r, c]);
                     
                    diccount++;
                }
                c = random.Next(0, Imagecol);
               r = random.Next(0, Imagerow);
            }
            //布局
            for (int i = 0; i < panel1.Controls.Count; i++)
            {
                PictureBox p = panel1.Controls[i] as PictureBox;
               
                p.Image = null;
                if (i!=0)
                {
                    p.Image = dic[(i - 1).ToString()]; 
                }
                
            }
            label1.Text = "你共走了 " + stepCount.ToString() + " 步！";
        }
    }
}
